package com.hong.utils.executors;

import java.util.concurrent.*;

/**
 * @Auther: Administrator
 * @Date: 2018/7/28 21:15
 * @Description:
 */
public class CompletionServiceExample2 {
    /**
     * CompletionService 解决了多线程结果同步返回
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*ExecutorService service = Executors.newFixedThreadPool(2);
        final List<Callable<Integer>> callableList = Arrays.asList(
                () -> {
                    sleep(10);
                    System.out.println("The 10 finished.");
                    return 10;
                }, () -> {
                    sleep(20);
                    System.out.println("The 20 finished.");
                    return 20;
                }
        );

        ExecutorCompletionService<Integer> completionService = new ExecutorCompletionService<>(service);

        List<Future<Integer>> futures = new ArrayList<>();
        callableList.stream().forEach(callable->futures.add(completionService.submit(callable)));*/

//        Future<Integer> future;
       /* while ((future =completionService.take())!=null){
            System.out.println(future.get());
        }*/
        /*Future<Integer> future = completionService.poll();
        System.out.println(future);*/
//        System.out.println(completionService.poll(11, TimeUnit.SECONDS).get());
        ExecutorService service = Executors.newFixedThreadPool(2);
        ExecutorCompletionService<Event>  completionService = new ExecutorCompletionService<Event>(service);
        final Event event = new Event(1);
        completionService.submit(new MyTask(event),event);
        System.out.println(completionService.take().get().result);

    }

    private static class MyTask implements Runnable{
        private final Event event;

        private MyTask(Event event) {
            this.event = event;
        }

        @Override
        public void run() {
            sleep(10);
            event.setResult("I AM SUCCESSFULLY.");
        }
    }

    private static class Event{
      final   private int eventId;
         private String result;


        private Event(int eventId, String result) {
            this.eventId = eventId;
            this.result = result;
        }

        public Event(int eventId) {
            this.eventId = eventId;
        }

        public int getEventId() {
            return eventId;
        }

        public String getResult() {
            return result;
        }

        public void setResult(String s) {
            this.result  = s;
        }
    }

    public static void sleep(long seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
